package com.click369.mianshiqingbao.controller;

import com.click369.mianshiqingbao.pojo.entity.Result;
import com.click369.mianshiqingbao.util.MianShiQingBaoHelper;
import com.click369.mianshiqingbao.util.StaticFinalFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 短信验证码服务的控制器类
 */
@Controller
@RequestMapping("/smsservice")
public class SMSServiceController {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 发送短信验证码
     *
     * @param phoneNum 接受短信验证码的手机号
     * @return 发送短信验证码成功或失败的封装类对象
     */
    @RequestMapping("/sendsms.do")
    @ResponseBody
    public Result sendSMS(String phoneNum) {
        //1.判断手机号码
        if (!MianShiQingBaoHelper.panDuanPhoneNum(phoneNum)) {
            return new Result(false, StaticFinalFile.ISNOTPHONENUM);
        }
            //2.生成手机验证码
            String code = MianShiQingBaoHelper.generateCode();
            //需要保存code，放在缓存中[Redis]
            redisTemplate.boundValueOps(phoneNum).set(code);
            System.out.println("yanzhengma=="+code);
            /*
            //3.将手机号码和验证码封装到Map集合
            Map<String, String> map = new HashMap<String, String>();
            map.put("phoneNum", phoneNum);
            map.put("param", "{\"code\":\"" + code + "\"}");
            try {
                SendSmsResponse response = SmsDemo.sendSms(map);
                Thread.sleep(3000L);
                if (response.getCode() != null && response.getCode().equals("OK")) {
                    return new Result(true, StaticFinalFile.SUCCESS);
                } else {
                    return new Result(false, StaticFinalFile.FAILURE);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, StaticFinalFile.FAILURE);
            }
            */
        return new Result(true, StaticFinalFile.SUCCESS);
        }

    }
